#include "node_item.h"

NodeItemBase::NodeItemBase(QString node_name)
    :QGraphicsItem()
{

    this->title = node_name;
    this->title_color = std::make_shared<QColor>("#aaaaaa");
    this->text = std::make_shared<QGraphicsTextItem>(this->title,this);
    this->text->setDefaultTextColor(*this->title_color);
    this->text->setPos(4,2);
    this->proxyContent = std::make_shared<QGraphicsProxyWidget>(this);
    this->setFlag(QGraphicsItem::ItemIsSelectable);
    this->setFlag(QGraphicsItem::ItemIsMovable);
}
NodeItemBase::NodeItemBase(const NodeItemBase &node_item){
    this->text = node_item.text;
    this->title_color = node_item.title_color;
    this->proxyContent = node_item.proxyContent;
}
NodeItemBase::NodeItemBase(const NodeItemBase &&node_item){
    Q_UNUSED(node_item);
}
void NodeItemBase::operator=(const NodeItemBase &node_item){
    this->text = node_item.text;
    this->title_color = node_item.title_color;
    this->proxyContent = node_item.proxyContent;
}
NodeItemBase::~NodeItemBase(){
    text.reset();
    title_color.reset();
    proxyContent.reset();
}
void NodeItemBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    Q_UNUSED(option);
    Q_UNUSED(widget);
    QPainterPath *path_title = new QPainterPath();
    path_title->setFillRule(Qt::WindingFill);
    path_title->addRoundedRect(0,0,width,title_height,edge_size,edge_size);//创建椭圆
    path_title->addRect(0,title_height-edge_size,edge_size,edge_size);//给椭圆左侧增加Rect遮住倒角
    path_title->addRect(width-edge_size,title_height-edge_size,edge_size,edge_size);//给椭圆右侧增加Rect遮住倒角
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#FF313131")); //E3212121
    painter->drawPath(path_title->simplified());

    QPainterPath *path_data = new QPainterPath();
    path_data->setFillRule(Qt::WindingFill);
    path_data->addRoundedRect(0,title_height,width,height-title_height,edge_size,edge_size);//创建椭圆
    path_data->addRect(0,title_height,edge_size,edge_size);//给椭圆左侧增加Rect遮住倒角
    path_data->addRect(width-edge_size,title_height,edge_size,edge_size);//给椭圆右侧增加Rect遮住倒角
    painter->setPen(Qt::NoPen);
    painter->setBrush(QColor("#E3212121"));
    painter->drawPath(path_data->simplified());



    QPainterPath *path_outline =new QPainterPath();
    path_outline->addRoundedRect(0,0,width,height,edge_size,edge_size);
    if(!this->isSelected()){
        painter->setPen(QPen(QColor("#aaaaaa")));
    }
    else{
        painter->setPen(QPen(QColor("#FFFFA637")));
    }
    painter->setBrush(Qt::NoBrush);
    painter->drawPath(path_outline->simplified());
}
QRectF NodeItemBase::boundingRect() const{
    return QRectF(
        0,
        0,
        width,
        height
                );
}

bool NodeItemBase::contains(const QPointF &point) const
{
    qreal right = this->pos().x() + width;
    qreal bottom = this->pos().y() + height;
    qreal left = this->pos().x();
    qreal top = this->pos().y();
    if(point.x()>left&&point.x()<right && point.y()>top&&point.y()<bottom){
        return true;
    }
    return false;

}






